package c2.group.business.teampvp;

import c2.advanced.business.base.AdvancedDaoTemplate;
import c2.advanced.share.DbFacade;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class TeamPvpDao extends AdvancedDaoTemplate {
    public static final String POOL_COLLECTION = "team-pvp-pool";
    public static final String TEAM_COLLECTION = "team-pvp-team";
    public static final String INHERIT_SCORE_COLLECTION = "team-pvp-inherit";
    public static final String INVITE_COLLECTION = "team-pvp-invites";

    public TeamPvpDao(DbFacade root) {
        super(root);
    }

    public Map<String, TeamPvpPlayer> getPoolPlayers() {
        long beginTime = System.currentTimeMillis();
        List<TeamPvpPlayer> list = this.mongo().findAll(TeamPvpPlayer.class, POOL_COLLECTION);
        log.info("组队竞技-玩家池拉取结束 ,耗时{}ms 共有{}个玩家", (System.currentTimeMillis() - beginTime), list.size());
        return list.stream().collect(Collectors.toMap(e -> e.ps.snapshot.uid, e -> e));
    }

    public Map<String, TeamPvpTeam> getTeams() {
        long beginTime = System.currentTimeMillis();
        List<TeamPvpTeam> list = this.mongo().findAll(TeamPvpTeam.class, TEAM_COLLECTION);
        log.info("组队竞技-队伍拉取结束 ,耗时{}ms 共有{}个队伍", (System.currentTimeMillis() - beginTime), list.size());
        return list.stream().collect(Collectors.toMap(e -> e.uid, e -> e));
    }

    public Map<String, TeamPvpInvite> getInvites() {
        long beginTime = System.currentTimeMillis();
        List<TeamPvpInvite> list = this.mongo().findAll(TeamPvpInvite.class, INVITE_COLLECTION);
        log.info("组队竞技-邀请拉取结束 ,耗时{}ms 共有{}条邀请", (System.currentTimeMillis() - beginTime), list.size());
        return list.stream().collect(Collectors.toMap(e -> e.uid, e -> e));
    }

    public Map<String, TeamPvpInherit> getInherits() {
        long beginTime = System.currentTimeMillis();
        List<TeamPvpInherit> list = this.mongo().findAll(TeamPvpInherit.class, INHERIT_SCORE_COLLECTION);
        log.info("组队竞技-继承池拉取结束 ,耗时{}ms 共有{}个玩家", (System.currentTimeMillis() - beginTime), list.size());
        return list.stream().collect(Collectors.toMap(e -> e.uid, e -> e));
    }

    public void savePlayers(Collection<TeamPvpPlayer> list) {
        long start = System.currentTimeMillis();
        DeleteResult dr = this.mongo().remove(new Query(), POOL_COLLECTION);
        log.info("组队竞技-玩家池更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<TeamPvpPlayer> r = this.mongo().insert(list, POOL_COLLECTION);
        log.info("组队竞技-玩家池试图insert {} 个数据, 实际插入 {} 个数据, cost={} ms", list.size(), r.size(), System.currentTimeMillis() -start);
    }

    public void saveTeams(Collection<TeamPvpTeam> list) {
        long start = System.currentTimeMillis();
        DeleteResult dr = this.mongo().remove(new Query(), TEAM_COLLECTION);
        log.info("组队竞技-队伍更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<TeamPvpTeam> r = this.mongo().insert(list, TEAM_COLLECTION);
        log.info("组队竞技-队伍试图insert {} 个数据, 实际插入 {} 个数据, cost={} ms", list.size(), r.size(), System.currentTimeMillis() - start);
    }

    public void saveInherit(Collection<TeamPvpInherit> list) {
        DeleteResult dr = this.mongo().remove(new Query(), INHERIT_SCORE_COLLECTION);
        log.info("组队竞技-继承池更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<TeamPvpInherit> r = this.mongo().insert(list, INHERIT_SCORE_COLLECTION);
        log.info("组队竞技-继承池试图insert {} 个数据, 实际插入 {} 个数据", list.size(), r.size());
    }

    public void saveInvites(Collection<TeamPvpInvite> list) {
        DeleteResult dr = this.mongo().remove(new Query(), INVITE_COLLECTION);
        log.info("组队竞技-邀请池更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<TeamPvpInvite> r = this.mongo().insert(list, INVITE_COLLECTION);
        log.info("组队竞技-邀请池试图insert {} 个数据, 实际插入 {} 个数据", list.size(), r.size());
    }
}
